package com.zy;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author zhaoyi
 * @date 2019/10/23
 * ReentrantLock 实现线程安全的买票
 */
public class Ticket {

    private int num = 100;

    private final ReentrantLock lock = new ReentrantLock();

    public void sale() {
        lock.lock();
        try {
            if (num > 0) {
                System.out.println(Thread.currentThread().getName()+" 线程出售一张票,后剩余 "+ (--num) + " 张票" );
            }
        } finally {
            lock.unlock();
        }
    }


}
